package com.lishe.lishe.retrofit;

import com.google.gson.Gson;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.utils.LogUtil;
import com.lishe.lishe.retrofit.MyRequestBody.DownLoadResponseBody;
import com.lishe.lishe.retrofit.interfaces.ProgressListener;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by DELL on 2017/11/20.
 */

public class RetrofitFactory {

    private static final long TIMEOUT = 20;
    //app 端接口
    private static final String BASE_URL = Globals.LISHE_URL;
    // 一企一舍接口
    private static final String BASE_URL2 = Globals.LISHE_URL_EXCITATION;

    public static final String APK_URL="https://admin.lishe.cn/Upload/appApk/";

    private static OkHttpClient httpClient = new OkHttpClient.Builder()
            .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String message) {
                    LogUtil.i("TAG", "body = " + message);
                }
            }).setLevel(HttpLoggingInterceptor.Level.BODY))
            .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(TIMEOUT, TimeUnit.SECONDS)
            .build();


    private static Retrofit.Builder builder = new Retrofit.Builder();


    public static Retrofit create() {
        return builder.baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())//通过ScalarsConverterFactory可以直接返回 Json 字符串
                .addConverterFactory(GsonConverterFactory.create(new Gson()))//通过GsonConverterFactory为Retrofit添加Gson支持
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//通过RxJava2CallAdapterFactory为Retrofit添加RxJava2支持
                .client(httpClient).build();
    }

    public static Retrofit createExcitationUrl() {
        return builder.baseUrl(BASE_URL2)
                .addConverterFactory(ScalarsConverterFactory.create())//通过ScalarsConverterFactory可以直接返回 Json 字符串
                .addConverterFactory(GsonConverterFactory.create(new Gson()))//通过GsonConverterFactory为Retrofit添加Gson支持
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//通过RxJava2CallAdapterFactory为Retrofit添加RxJava2支持
                .client(httpClient).build();
    }

    public static Retrofit createDownloadUrl(final ProgressListener listener){
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        LogUtil.i("TAG", "body = " + message);
                    }
                }).setLevel(HttpLoggingInterceptor.Level.BODY))
                .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
//                .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Response response=chain.proceed(chain.request());
                        return response.newBuilder().body(new DownLoadResponseBody(response.body(), new ProgressListener() {
                            @Override
                            public void onProgress(long progress, long total, boolean done) {
                                listener.onProgress(progress,total,done);
                            }
                        })).build();
                    }
                }).build();

        return builder
                .baseUrl(APK_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(httpClient).build();
    }
}
